package com.jt.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JDKProxy {
    public static Object getProxy(Object target) {
        ClassLoader loader = target.getClass().getClassLoader();
        Class<?>[] classs = target.getClass().getInterfaces();
        Object proxy = Proxy.newProxyInstance(loader, classs, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object result=null;
                try {
                    System.out.println("事务开始");
                    result = method.invoke(target, args);
                    System.out.println("事务结束");
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("事务回滚");
                }
                return null;
            }
        });

    return proxy;
    }
}
